package com.feeyo.groundservice.http.base

import com.feeyo.groundservice.base_util.FyLog
import com.lxj.xpopup.XPopup
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 *作者：QLH on 2023-09-19
 *描述：
 */
abstract class NetWorkObserver<T> : IObserver<T> {

    var t: T? = null
    override fun onStart() {
        onLoadingStart()
    }

    override fun onError(code: Int, msg: String) {
        FyLog.e("onError", "code: $code, msg: $msg")
        onLoadingEnd()
    }

    override fun onComplete(data: T?) {
        onLoadingEnd()
        t = data
        //延迟300ms
        GlobalScope.launch(Dispatchers.Main) {
            delay(XPopup.getAnimationDuration().toLong() + 200)
            onSuccess(t)
        }

    }

    abstract fun onSuccess(data: T?)

    //控制loading过程的
    abstract fun onLoadingStart()
    abstract fun onLoadingEnd()
}